using System;
using System.Linq;
using Icms.Core.Model;
using Icms.Core.Repository;
using Icms.Core.Service;

namespace Icms.Service
{
    public class UserService : CrudService<User>, IUserService
    {
        public UserService(IRepo<User> repo) : base(repo)
        {
        }

        public Guid Create(User o, Court court)
        {
            if (court != null) o.Court = court;
            return Create(o);
        }

        public void ChangePassword(Guid userId, string password)
        {
            repo.Get(userId).Password = password;
            repo.Save();
        }

        public override void Delete(Guid id)
        {
            var o = repo.Get(id);
            o.Roles.Count();
            repo.Delete(o);
            repo.Save();
        }

        public Court GetCourt(string  login)
        {
            var res = repo.Where(o => o.Login == login);
            return res.Count() == 0 ? null : res.Single().Court;
        }

        public User GetByLogin(string login)
        {
            var user = repo.Where(o => o.Login == login).SingleOrDefault();
            return user;
        }
    }
}